package xyz.yuzegod.megawalls.classes.novice.him;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.CollectSkill;
import xyz.yuzegod.megawalls.game.ChestManager;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FourthSkill extends CollectSkill
{
    public FourthSkill(final Classes classes) {
        super("宝藏猎人", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 1.4;
            }
            case 2: {
                return 1.7;
            }
            case 3: {
                return 2.0;
            }
            default: {
                return 1.4;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7挖矿时找到宝箱的几率提升§a" + StringUtils.percent(this.getAttribute(level)) + "§7。");
            return lore;
        }
        lore.add(" §8▪ §7挖矿时找到宝箱的几率提升§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill4Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill4Level();
    }
    
    @Override
    public void onBlockBreak(final KitStatsContainer kitStats, final BlockBreakEvent e) {
        Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
            @Override
            public void run() {
                if ((ItemUtils.exceptDirt(e.getBlock())) && e.getBlock().getType() != Material.TRAPPED_CHEST && MegaWalls.getRandom().nextInt(200) <= FourthSkill.this.getAttribute(kitStats.getSkill4Level()) * 10.0) {
                    final Block block = e.getBlock();
                    block.setType(Material.TRAPPED_CHEST);
                    GamePlayer.get(e.getPlayer().getUniqueId()).addProtectedBlock(block);
                    final Chest chest = (Chest) block.getState();
                    ChestManager.fillInventory(e.getPlayer(), chest.getBlockInventory());
                }
            }
        }, 5L);
    }
}
